<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;

class CollectsController extends BaseController{

    //我的收藏 旅游列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function getTourCollectionList($user_id){

        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);

		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}
        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //取得收藏商品
        $query = DB::table('collect')
            ->join('tour', 'collect.item_id', '=', 'tour.tour_id')
            ->join('tour_statistics', 'tour_statistics.tour_id', '=', 'tour.tour_id')
            ->where('collect.user_id',$user_id)
            ->where('collect.type','=','tour');

        if($lat==999 && $lon == 999){
            $query->select(DB::raw("round(6378.138*2*asin(sqrt(pow(sin(($lat*pi()/180-latitude*pi()/180)/2),2)+cos($lat*pi()/180)*cos(latitude*pi()/180)* pow(sin( ($lon*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance,
            province,city,country"),
                'tour.tour_id','tour.store_id','tour.tour_name','tour.price_a','tour.market_price_a',
                'tour.short_name','tour.default_image','tour.default_image2','tour_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        } else {
            $query->select(DB::raw("-1 as distance,
            province,city,country"),
                'tour.tour_id','tour.store_id','tour.tour_name','tour.price_a','tour.market_price_a',
                'tour.short_name','tour.default_image','tour.default_image2','tour_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        }
            
        $collects = $query->get();

        //编辑默认图片
        foreach ($collects as $collect_item)
        {
            $collect_item->default_image = complete_url($collect_item->default_image);
            $collect_item->default_image2 = complete_url($collect_item->default_image2);
        }
        
        return $this->success($collects?$collects:[]);
    }

    //我的收藏 商品列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function getGoodsCollectionList($user_id){

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //取得收藏商品
        $query = DB::table('collect')
            ->join('goods', 'collect.item_id', '=', 'goods.goods_id')
            ->join('goods_statistics', 'goods_statistics.goods_id', '=', 'goods.goods_id')
            ->where('collect.user_id',$user_id)
            ->where('collect.type','=','goods')
            ->select('goods.goods_id','goods.store_id','goods.goods_name','goods.price','goods.market_price',
                'goods.short_name','goods.default_image','goods.default_image2','goods_statistics.sales')
            ->skip($firstrow)->take($pagesize);

        $collects = $query->get();

        //编辑默认图片
        foreach ($collects as $collect_item)
        {
            $collect_item->default_image = complete_url($collect_item->default_image);
            $collect_item->default_image2 = complete_url($collect_item->default_image2);
        }

        return $this->success($collects?$collects:[]);
    }

    //我的收藏 民宿列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function getHotelCollectionList($user_id){

        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);

		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}
		
        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //取得收藏商品
        $query = DB::table('collect')
            ->join('hotels', 'collect.item_id', '=', 'hotels.hotels_id')
            ->join('hotels_statistics', 'hotels_statistics.hotels_id', '=', 'hotels.hotels_id')
            ->where('collect.user_id',$user_id)
            ->where('collect.type','=','hotel');

        if($lat==999 && $lon == 999){
            $query->select(DB::raw("round(6378.138*2*asin(sqrt(pow(sin(($lat*pi()/180-latitude*pi()/180)/2),2)+cos($lat*pi()/180)*cos(latitude*pi()/180)* pow(sin( ($lon*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance,
            province,city,country"),
                'hotels.hotels_id','hotels.store_id','hotels.hotels_name','hotels.price','hotels.market_price',
                'hotels.short_name','hotels.default_image','hotels.default_image2','hotels_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        } else {
            $query->select(DB::raw("-1 as distance,
            province,city,country"),
                'hotels.hotels_id','hotels.store_id','hotels.hotels_name','hotels.price','hotels.market_price',
                'hotels.short_name','hotels.default_image','hotels.default_image2','hotels_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        }

        $collects = $query->get();

        //编辑默认图片
        foreach ($collects as $collect_item)
        {
            $collect_item->default_image = complete_url($collect_item->default_image);
            $collect_item->default_image2 = complete_url($collect_item->default_image2);
        }

        return $this->success($collects?$collects:[]);
    }

    //我的收藏 农家乐列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function getRestaurantsCollectionList($user_id){

        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);
		
		if($lat=='undefined'){
			$lat = 999;
		}
		if($lon=='undefined'){
			$lon = 999;
		}

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //取得收藏商品
        $query = DB::table('collect')
            ->join('restaurants', 'collect.item_id', '=', 'restaurants.restaurants_id')
            ->join('restaurants_statistics', 'restaurants_statistics.restaurants_id', '=', 'restaurants.restaurants_id')
            ->where('collect.user_id',$user_id)
            ->where('collect.type','=','restaurant');

        if($lat==999 && $lon == 999){
            $query->select(DB::raw("round(6378.138*2*asin(sqrt(pow(sin(($lat*pi()/180-latitude*pi()/180)/2),2)+cos($lat*pi()/180)*cos(latitude*pi()/180)* pow(sin( ($lon*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance,
            province,city,country"),
                'restaurants.restaurants_id','restaurants.store_id','restaurants.restaurants_name','restaurants.price','restaurants.market_price',
                'restaurants.short_name','restaurants.default_image','restaurants.default_image2','restaurants_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        } else {
            $query->select(DB::raw("-1 as distance,
            province,city,country"),
                'restaurants.restaurants_id','restaurants.store_id','restaurants.restaurants_name','restaurants.price','restaurants.market_price',
                'restaurants.short_name','restaurants.default_image','restaurants.default_image2','restaurants_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        }

        $collects = $query->get();
        //编辑默认图片
        foreach ($collects as $collect_item)
        {
            $collect_item->default_image = complete_url($collect_item->default_image);
            $collect_item->default_image2 = complete_url($collect_item->default_image2);
        }

        return $this->success($collects?$collects:[]);
    }
    //我的收藏 做地主列表
    /**
     * @param $user_id
     * @return mixed
     */
    public function getLandownersCollectionList($user_id){
        $lat =Input::get('lat',999);
        $lon =Input::get('lon',999);

        if($lat=='undefined'){
            $lat = 999;
        }
        if($lon=='undefined'){
            $lon = 999;
        }

        $firstrow = Input::get('firstrow',0);
        $pagesize = Input::get('pagesize',10);

        //取得收藏商品
        $query = DB::table('collect')
            ->join('landowners', 'collect.item_id', '=', 'landowners.landowners_id')
            ->leftJoin('landowners_statistics', 'landowners_statistics.landowners_id', '=', 'landowners.landowners_id')
            ->where('collect.user_id',$user_id)
            ->where('collect.type','=','landowners');

        if($lat==999 && $lon == 999){
            $query->select(DB::raw("round(6378.138*2*asin(sqrt(pow(sin(($lat*pi()/180-latitude*pi()/180)/2),2)+cos($lat*pi()/180)*cos(latitude*pi()/180)* pow(sin( ($lon*pi()/180-longitude*pi()/180)/2),2)))*1000) as distance,
            province,city,country"),
                'landowners.landowners_id','landowners.store_id','landowners.landowners_name','landowners.price','landowners.market_price',
                'landowners.short_name','landowners.default_image','landowners.default_image2','landowners_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        } else {
            $query->select(DB::raw("-1 as distance,
            province,city,country"),
                'landowners.landowners_id','landowners.store_id','landowners.landowners_name','landowners.price','landowners.market_price',
                'landowners.short_name','landowners.default_image','landowners.default_image2','landowners_statistics.sales')
                ->skip($firstrow)->take($pagesize);
        }

        $collects = $query->get();
        //编辑默认图片
        foreach ($collects as $collect_item)
        {
            $collect_item->default_image = complete_url($collect_item->default_image);
            $collect_item->default_image2 = complete_url($collect_item->default_image2);
        }

        return $this->success($collects?$collects:[]);
    }


    //收藏线路
    /**
     * 收藏商品
     */
    public function collectTours($tour_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$tour_id){
            return $this->args_invalid();
        }

        /* 验证要收藏的商品是否存在 */
        $count =DB::table('tour')
            ->where('tour_id',$tour_id)
            ->count();

        if(!$count){
            return $this->failure('该线路不存在');
        }

        DB::beginTransaction();

        try {
            /* 更新收藏表 */
            unset($count);
            $count = DB::table('collect')
                ->where('user_id',$user_id)
                ->where('type','tour')
                ->where('item_id',$tour_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'tour',
                        'item_id'=>$tour_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );

                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','tour')
                    ->where('item_id',$tour_id)->delete();

                $collect_status=0;
                $message = "取消收藏";
            }

            /* 更新被收藏次数 */
            unset($count);
            $count = DB::table('collect')
                ->where('type','tour')
                ->where('item_id',$tour_id)
                ->count();

            DB::table('tour_statistics')
                ->where('tour_id',$tour_id)
                ->update(array('collects'=>$count));

            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
    }

    //收藏商品
    /**
     * 收藏商品
     */
    public function collectGoods($goods_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$goods_id){
            return $this->args_invalid();
        }

        /* 验证要收藏的商品是否存在 */
        $count =DB::table('goods')
            ->where('goods_id',$goods_id)
            ->count();

        if(!$count){
            return $this->failure('该商品不存在');
        }

        DB::beginTransaction();

        try {
            /* 更新收藏表 */
            unset($count);
            $count = DB::table('collect')
                ->where('user_id',$user_id)
                ->where('type','goods')
                ->where('item_id',$goods_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'goods',
                        'item_id'=>$goods_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );

                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','goods')
                    ->where('item_id',$goods_id)->delete();

                $collect_status=0;
                $message = "取消收藏";
            }

            /* 更新被收藏次数 */
            unset($count);
            $count = DB::table('collect')
                ->where('type','goods')
                ->where('item_id',$goods_id)
                ->count();

            DB::table('goods_statistics')
                ->where('goods_id',$goods_id)
                ->update(array('collects'=>$count));

            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
    }

    //收藏民宿
    /**
     * 收藏商品
     */
    public function collectHotels($hotels_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$hotels_id){
            return $this->args_invalid();
        }

        /* 验证要收藏的商品是否存在 */
        $count =DB::table('hotels')
            ->where('hotels_id',$hotels_id)
            ->count();

        if(!$count){
            return $this->failure('该民宿不存在');
        }

        DB::beginTransaction();

        try {
            /* 更新收藏表 */
            unset($count);
            $count = DB::table('collect')
                ->where('user_id',$user_id)
                ->where('type','hotel')
                ->where('item_id',$hotels_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'hotel',
                        'item_id'=>$hotels_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );

                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','hotel')
                    ->where('item_id',$hotels_id)->delete();

                $collect_status=0;
                $message = "取消收藏";
            }

            /* 更新被收藏次数 */
            unset($count);
            $count = DB::table('collect')
                ->where('type','hotel')
                ->where('item_id',$hotels_id)
                ->count();

            DB::table('hotels_statistics')
                ->where('hotels_id',$hotels_id)
                ->update(array('collects'=>$count));

            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
    }

    //收藏农家乐
    /**
     * 收藏商品
     */
    public function collectRestaurant($restaurants_id){

        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$restaurants_id){
            return $this->args_invalid();
        }

        /* 验证要收藏的商品是否存在 */
        $count =DB::table('restaurants')
            ->where('restaurants_id',$restaurants_id)
            ->count();

        if(!$count){
            return $this->failure('该农家乐不存在');
        }

        DB::beginTransaction();

        try {
            /* 更新收藏表 */
            unset($count);
            $count = DB::table('collect')
                ->where('user_id',$user_id)
                ->where('type','restaurant')
                ->where('item_id',$restaurants_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'restaurant',
                        'item_id'=>$restaurants_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );

                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','restaurant')
                    ->where('item_id',$restaurants_id)->delete();

                $collect_status=0;
                $message = "取消收藏";
            }

            /* 更新被收藏次数 */
            unset($count);
            $count = DB::table('collect')
                ->where('type','restaurant')
                ->where('item_id',$restaurants_id)
                ->count();

            DB::table('restaurants_statistics')
                ->where('restaurants_id',$restaurants_id)
                ->update(array('collects'=>$count));

            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
    }

    //我的收藏 做地主
    /**
     * @param $user_id
     * @return mixed
    */
    public function collectLandowners($landowners_id){
        $collect_status=0;

        $user_id = Input::get('user_id');

        //参数编辑
        if (!$landowners_id){
            return $this->args_invalid();
        }

        /* 验证要收藏的商品是否存在 */
        $count =DB::table('landowners')
            ->where('landowners_id',$landowners_id)
            ->count();

        if(!$count){
            return $this->failure('不存在');
        }

        DB::beginTransaction();

        try {
            /* 更新收藏表 */
            unset($count);
            $count = DB::table('collect')
                ->where('user_id',$user_id)
                ->where('type','landowners')
                ->where('item_id',$landowners_id)->count();
            $message = "收藏成功";
            if(!$count){
                DB::table('collect')->insert(
                    array(
                        'user_id' => $user_id,
                        'type' => 'landowners',
                        'item_id'=>$landowners_id,
                        'keyword'=>'',
                        'add_time' =>gmtime())
                );

                $collect_status=1;
            } else {
                DB::table('collect')
                    ->where('user_id',$user_id)
                    ->where('type','landowners')
                    ->where('item_id',$landowners_id)->delete();

                $collect_status=0;
                $message = "取消收藏";
            }

            /* 更新被收藏次数 */
            unset($count);
            $count = DB::table('collect')
                ->where('type','landowners')
                ->where('item_id',$landowners_id)
                ->count();

            DB::table('landowners_statistics')
                ->where('landowners_id',$landowners_id)
                ->update(array('collects'=>$count));

            DB::commit();

            return $this->success(['collect_status'=>$collect_status],$message);

        } catch (Exception $e) {
            DB::rollback();
            throw $e;
        }
    }

//    //我的收藏 拼团列表
//    /**
//     * @param $user_id
//     * @return mixed
//     */
//    public function getPintuanCollectoinList($user_id){
//
//        $pintuantype = Input::get('pintuan_type','1'); //拼团类型 1-普通拼团 2-秒杀拼团 3-抽奖拼团
//        $firstrow = Input::get('firstrow',0);
//        $pagesize = Input::get('pagesize',10);
//
//        if(!$pintuantype || !in_array($pintuantype,[PINTUAN_PUTONG,PINTUAN_MIAOSHA,PINTUAN_CHOUJIANG])){
//            return $this->args_invalid();
//        }
//
//        $query = DB::table('collect')
//                ->join('pintuan', 'collect.item_id', '=', 'pintuan.pintuan_id')
//                ->leftJoin('pintuan_statistics','pintuan.pintuan_id','=','pintuan_statistics.pintuan_id');
//
//        $data = [];
//        switch($pintuantype)
//        {
//            case PINTUAN_PUTONG:
//
//                $query->where('collect.user_id',$user_id)
//                    ->where('collect.type','=','pintuan')
//                    ->where('pintuan.pintuan_type','=',$pintuantype)
//                    ->select('pintuan.pintuan_id','pintuan.store_id','pintuan.pintuan_name','pintuan.pintuan_desc',
//                        'pintuan.pt_price','pintuan.min_quantity', 'pintuan.if_show', 'pintuan.pintuan_type','pintuan.pintuan_logo',
//                        'pintuan_statistics.sales_cnt',"pintuan.limit_count");
//
//                //取得收藏商品
//                $query->skip($firstrow)->take($pagesize);
//                $collects = $query->get();
//
//                //数据格式调整
//                $data = [];
//                foreach($collects as $pintuan){
//                    $temp = [
//                        'pintuan_id'    => $pintuan->pintuan_id,
//                        'pintuan_name'  => $pintuan->pintuan_name,
//                        'pintuan_logo'  => complete_url($pintuan->pintuan_logo),
//                        'pt_price'      => $pintuan->pt_price,
//                        'sales_cnt'     => $pintuan->sales_cnt,
//                        'min_quantity'  => $pintuan->min_quantity
//                    ];
//
//                    $data[] = $temp;
//                }
//
//                break;
//            case PINTUAN_MIAOSHA:
//
//                $query->join('pintuan_ms','pintuan.pintuan_id','=','pintuan_ms.pintuan_id')
//                    ->leftJoin('pintuan_spec','pintuan_spec.pt_spec_id','=','pintuan.default_pt_spec')
//                    ->leftJoin('goods_spec','pintuan_spec.spec_id','=','goods_spec.spec_id')
//                    ->where('collect.user_id',$user_id)
//                    ->where('collect.type','=','pintuan')
//                    ->where('pintuan.pintuan_type','=',$pintuantype)
//                    ->select('pintuan.pintuan_id','pintuan.store_id','pintuan.pintuan_name','pintuan.pintuan_desc',
//                        'pintuan.pt_price','pintuan.min_quantity', 'pintuan.if_show', 'pintuan.pintuan_type','pintuan.pintuan_logo',
//                        'pintuan_statistics.sales_cnt','pintuan_ms.start_time','pintuan_ms.end_time',"goods_spec.market_price",
//                        "pintuan.limit_count");
//
//                //取得收藏商品
//                $query->skip($firstrow)->take($pagesize);
//                $collects = $query->get();
//
//                //数据格式调整
//
//                foreach($collects as $pintuan) {
//                    //只取未开始和已经开始秒杀活动的拼团，时间过了的秒杀活动不显示
//                    $rest_count = $pintuan->limit_count - $pintuan->sales_cnt;
//
//                    $rest_tmie = $pintuan->start_time - gmtime();
//                    if ($rest_tmie > 0) {
//                        $state = PINTUAN_MIAOSHA_WAITE;
//                        $state_desc = date('m月d日 H:i', $pintuan->start_time) . " 准时开抢";
//                    } else {
//                        $rest_tmie = 0;
//                        if ($rest_count > 0) {
//                            $state = PINTUAN_MIAOSHA_MIAOSHAING;
//                            $state_desc = "剩余" . $rest_count . "件";
//                        } else {
//                            $state = PINTUAN_MIAOSHA_SELL_OUT;
//                            $state_desc = "已售罄";
//                        }
//                    }
//
//                    //编辑默认图片
//                    $pintuan->pintuan_logo = complete_url($pintuan->pintuan_logo);
//
//                    $temp = [
//                        'pintuan_id' => $pintuan->pintuan_id,
//                        'pintuan_name' => $pintuan->pintuan_name,
//                        'pintuan_logo' => complete_url($pintuan->pintuan_logo),
//                        'pt_price' => $pintuan->pt_price,
//                        'market_price' => $pintuan->market_price,
//                        'rest_count' => $rest_count,
//                        'rest_time' => $rest_tmie,
//                        'miaosha_state' => $state,
//                        'miaosha_state_desc' => $state_desc
//                    ];
//
//                    $data[] = $temp;
//                }
//
//                break;
//            case PINTUAN_CHOUJIANG:
//
//                $query->join('pintuan_cj','pintuan.pintuan_id','=','pintuan_cj.pintuan_id')
//                    ->leftJoin('pintuan_spec','pintuan_spec.pt_spec_id','=','pintuan.default_pt_spec')
//                    ->leftJoin('goods_spec','pintuan_spec.spec_id','=','goods_spec.spec_id')
//                    ->where('collect.user_id',$user_id)
//                    ->where('collect.type','=','pintuan')
//                    ->where('pintuan.pintuan_type','=',$pintuantype)
//                    ->select('pintuan.pintuan_id','pintuan.store_id','pintuan.pintuan_name','pintuan.pintuan_desc',
//                        'pintuan.pt_price','pintuan.min_quantity', 'pintuan.if_show', 'pintuan.pintuan_type','pintuan.pintuan_logo',
//                        'pintuan_statistics.sales_cnt','pintuan_cj.start_time','pintuan_cj.end_time','pintuan_cj.dj_status',"goods_spec.market_price",
//                        "pintuan.limit_count");
//
//                //取得收藏商品
//                $query->skip($firstrow)->take($pagesize);
//                $collects = $query->get();
//
//                //数据格式调整
//                foreach($collects as $pintuan){
//
//                    $temp = [
//                        'pintuan_id'    => $pintuan->pintuan_id,
//                        'pintuan_name'  => $pintuan->pintuan_name,
//                        'pintuan_logo'  => complete_url($pintuan->pintuan_logo),
//                        'pt_price'      => $pintuan->pt_price,
//                        'market_price'  => $pintuan->market_price,
//                        'start_time' => $pintuan->start_time,
//                        'end_time'    => $pintuan->end_time,
//                        'dj_status' => $pintuan->dj_status,
//                        'pintuan_type'    => $pintuan->pintuan_type
//                    ];
//
//                    $data[] = $temp;
//                }
//
//                break;
//            default:
//                break;
//        }
//
//        return $this->success($data);
//    }
}